package com.cmy.lesson01;

import java.sql.*;

public class JdbcFirst {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");// 固定写法,加载驱动

        //用户信息 url 主机地址,数据库名 ?连接参数
        // useUnicode=true(支持中文) &分隔多个参数
        // characterEncoding=utf8 (使用utf8字符集,防止中文乱码)
        // useSSL=true使用安全连接
        String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true";
        String username = "root";
        String userpwd = "123456";

        //连接成功,返回数据库对象 connection 代表数据库
        Connection connection = DriverManager.getConnection(url, username, userpwd);

        //执行SQL的对象 statement
        Statement statement = connection.createStatement();

        //执行SQL的对象 去 执行SQL,可能存在结果,查看返回结果
        String sql = "SELECT * FROM users";
        ResultSet resultSet = statement.executeQuery(sql);//返回结果集(封装了全部的查询结果),链表

        while(resultSet.next()){
            System.out.println("id = "+resultSet.getObject("id"));
            System.out.println("name = "+resultSet.getObject("NAME"));
            System.out.println("password = "+resultSet.getObject("PASSWORD"));
            System.out.println("email = "+resultSet.getObject("email"));
            System.out.println("birthday = "+resultSet.getObject("birthday"));
        }

        //释放连接(顺序为建立倒序)
        resultSet.close();
        statement.close();
        connection.close();
    }
}

    